package com.rz.paas.test

import android.Manifest
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.orhanobut.hawk.Hawk
import com.rz.paas.test.SettingDetail.CameraDetectionActivity
import com.rz.paas.test.SettingDetail.CustomPushActivity
import com.rz.paas.test.SettingDetail.MicrophoneDetectionActivity
import com.rz.paas.test.SettingDetail.SettingDetailActivity
import com.rz.paas.test.base.MiddleActivity
import com.rz.paas.test.camera.PermissionUtil
import com.rz.paas.test.constant.AppConstant
import kotlinx.android.synthetic.main.activity_setting.*
import kotlinx.android.synthetic.main.custom_action_bar.*


class SettingActivity : MiddleActivity() {

    companion object {

    }

    val dataMap = AppConstant.dataMap
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting)

        main_title.text = "设置"

        env_select.setOnClickListener {
            val optionIndex = AppConstant.ENV_DATA_INDEX
            SettingDetailActivity.open(
                this, optionIndex, Hawk.get(dataMap[optionIndex].name) ?: 0
            )
        }

        encode_mode_select.setOnClickListener {
            val optionIndex = AppConstant.ENCODE_MODE_DATA_INDEX
            SettingDetailActivity.open(
                this, optionIndex, Hawk.get(dataMap[optionIndex].name) ?: 0
            )
        }

        render_select.setOnClickListener {
            val optionIndex = AppConstant.VIDEO_RENDER_TYPE_DATA_INDEX
            SettingDetailActivity.open(
                this, optionIndex, Hawk.get(dataMap[optionIndex].name) ?: 0
            )
        }

        render_mode_select.setOnClickListener {
            val optionIndex = AppConstant.RENDER_MODE_INDEX
            SettingDetailActivity.open(
                this, optionIndex, Hawk.get(dataMap[optionIndex].name) ?: 0
            )
        }

        default_push_select.setOnClickListener {
            CustomPushActivity.open(this, false)

        }
        custom_push_select.setOnClickListener {
            CustomPushActivity.open(this, true)
        }






        initText()

        checkPermissions()
    }


    private val PERMISSION_CODE = 1000
    private fun checkPermissions() {
        checkMicrophonePermission()
        checkCameraPermission()
    }

    private fun checkCameraPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED
        ) {
            cameraPermission(true)
            camera_authority_select.setOnClickListener {
                requestPermission(true)
            }
        } else {
            cameraPermissionGenerated()
        }

    }

    private fun checkMicrophonePermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
            != PackageManager.PERMISSION_GRANTED
        ) {
            microphonePermission(true)
            microphone_authority_select.setOnClickListener {
                requestPermission(false)
            }
        } else {
            microphonePermissionGenerated()
        }
    }

    private fun requestPermission(camera: Boolean) {
        if (camera) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE),
                PERMISSION_CODE
            )

        } else {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.RECORD_AUDIO),
                PERMISSION_CODE
            )
        }

    }

    private fun cameraPermissionGenerated() {
        cameraPermission(false)

        camera_authority_select.setOnClickListener {
            CameraDetectionActivity.open(this)
        }
    }

    private fun microphonePermissionGenerated() {
        microphonePermission(false)
        microphone_authority_select.setOnClickListener {
            MicrophoneDetectionActivity.open(this)
        }
    }

    private fun initText() {
        current_env.text = getResString(AppConstant.ENV_DATA_INDEX)
        current_encode_mode.text = getResString(AppConstant.ENCODE_MODE_DATA_INDEX)
        current_render.text = getResString(AppConstant.VIDEO_RENDER_TYPE_DATA_INDEX)
        current_render_mode.text = getResString(AppConstant.RENDER_MODE_INDEX)
    }

    private fun getResString(dataIndex: Int): String {
        var stringArray = resources.getStringArray(dataMap[dataIndex].resId)
        var selectIndex = Hawk.get(dataMap[dataIndex].name, 0)
        if (selectIndex >= stringArray.size && dataIndex == AppConstant.ENV_DATA_INDEX) {
            return "自定义"
        }
        return stringArray[selectIndex]
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            val optionId = data!!.getIntExtra(SettingDetailActivity.OPTION_ID, 0)
            when (optionId) {
                AppConstant.ENV_DATA_INDEX ->
                    current_env.text = getResString(optionId)
                AppConstant.ENCODE_MODE_DATA_INDEX ->
                    current_encode_mode.text = getResString(optionId)
                AppConstant.VIDEO_RENDER_TYPE_DATA_INDEX ->
                    current_render.text = getResString(optionId)
                AppConstant.RENDER_MODE_INDEX ->
                    current_render_mode.text = getResString(optionId)

            }
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        for (i in permissions.indices) {
            val permission = permissions[i]!!
            val result = grantResults[i]
            if (permission == Manifest.permission.RECORD_AUDIO) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    goSetting(false)
                } else {
                    microphonePermissionGenerated()
                }
            } else if (permission == Manifest.permission.CAMERA) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    goSetting(true)
                } else {
                    cameraPermissionGenerated()

                }
            }

        }

    }

    private fun goSetting(camera: Boolean) {
        var alertDialog = AlertDialog.Builder(this)
            .setMessage("必须的权限没有得到授权，功能将无法使用 请重新授权!")
            .setNegativeButton(
                "不"
            ) { dialog, which ->
                dialog.dismiss()
                finish()
            }
            .setPositiveButton(
                "好的"
            ) { dialog, which ->
                PermissionUtil.gotoPermission(baseContext)

//                requestPermission(camera)
            }.create()
        alertDialog.show()

        var button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
        button.setTextColor(ContextCompat.getColor(this, R.color.color_bg_blue))
        alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE)
            .setTextColor(ContextCompat.getColor(this, R.color.color_bg_blue))

    }

    fun cameraPermission(denied: Boolean) {
        open_camera_permission.visibility = if (denied) View.VISIBLE else View.INVISIBLE
        current_camera_authority.visibility = if (denied) View.INVISIBLE else View.VISIBLE
    }

    fun microphonePermission(denied: Boolean) {
        open_microphone_permission.visibility = if (denied) View.VISIBLE else View.INVISIBLE
        current_microphone_authority.visibility = if (denied) View.INVISIBLE else View.VISIBLE
    }


}